
$(document).ready(function(){
	$('#relativeIncome').hide();//收入环比
	$('#relativeOutcome').hide();//支出环比
	$('#relativeProfit').hide();//利润环比
	$('#sameRatioIncome').hide();//收入同比
	$('#sameRatioOutcome').hide();//支出同比
	$('#sameRatioProfit').hide();//利润同比
	loadAll(0);
});



// 加载所有
function loadAll(dateType){
	$.ajax({
		type: "POST",
		url: '/web/bi/business/getInfoByFlowAnalysis',
		data: {"dateType":dateType},
		async: true,
		success: function (data) {
			console.log('data',data);
			if (data.code == 0) {
				//刷新时间
				$('#lastTime').text("("+data.lastTime+")");

				//收入
				$('#totalIncome').text(data.totalIncome);
				$('#relativeIncome').text("环比："+data.relativeIncome+"%");
				$('#sameRatioIncome').text("同比："+data.sameRatioIncome+"%");
				//支出
				$('#totalOutcome').text(data.totalOutcome);
				$('#relativeOutcome').text("环比："+data.relativeOutcome+"%");
				$('#sameRatioOutcome').text("同比："+data.sameRatioOutcome+"%");
				//利润
				$('#totalProfit').text(data.totalProfit);
				$('#relativeProfit').text("环比："+data.relativeProfit+"%");
				$('#sameRatioProfit').text("同比："+data.sameRatioProfit+"%");

				var itemArrByIncome = [];
				var itemArrByOutcome = [];
				var itemArrByProfit = [];
				var valueArrByIncome = [];
				var valueArrByOutcome = [];
				var valueArrByProfit = [];

				var echarList = data.echarList;
				if(echarList!=null && echarList.length>0){
					for(var i=0;i<echarList.length;i++){
						var row = echarList[i];
						var dataType = row.dataType;
						if(row.amount!=0){
							if(dataType==0){
								itemArrByIncome.push(row.integrityType);
								valueArrByIncome.push({value: row.amount, name:row.integrityType});
							}else if(dataType==1){
								itemArrByOutcome.push(row.integrityType);
								valueArrByOutcome.push({value: row.amount, name:row.integrityType});
							}else if(dataType==2){
								itemArrByProfit.push(row.integrityType);
								if(row.amount>0){
									valueArrByProfit.push({value: row.amount, name:row.integrityType});
								}else {
									valueArrByProfit.push({
										value: 0-row.amount,
										name:row.integrityType,
										tooltip:{
											formatter: '{a} <br/>{b} : -{c} ({d}%)'
										}
									});
								}

							}else {

							}
						}

					}
				}
				loadShouru(itemArrByIncome,valueArrByIncome); // 初始加载收入饼状图
				loadZhichu(itemArrByOutcome,valueArrByOutcome); // 初始加载支出饼状图
				loadLirun(itemArrByProfit,valueArrByProfit); // 初始加载利润饼状图
			} else {
				alert(data.msg);
			}
		}, error: function() {
			alert("操作失败，请稍后再试！", { icon: 2 });
		}
	});


}

function doReloadByDteType(dom,dateType){
	if(dateType!=3){
		for(var i=0;i<=2;i++){
			if(i==dateType){
				$('#dteType'+dateType).addClass("btn-selected");
			}else {
				$('#dteType'+i).removeClass("btn-selected")
			}
		}
	}

	if(dateType==3){
		//数据表
		window.top.add_tab("业务流水分析汇总表","/indexCommonOfMenu?menuId=10401");
	}else {
		loadAll(dateType);
		if(dateType==0){
			$('#relativeIncome').hide();//收入环比
			$('#relativeOutcome').hide();//支出环比
			$('#relativeProfit').hide();//利润环比
			$('#sameRatioIncome').hide();//收入同比
			$('#sameRatioOutcome').hide();//支出同比
			$('#sameRatioProfit').hide();//利润同比
		}else if(dateType==1){
			$('#relativeIncome').show();//收入环比
			$('#relativeOutcome').show();//支出环比
			$('#relativeProfit').show();//利润环比
			$('#sameRatioIncome').show();//收入同比
			$('#sameRatioOutcome').show();//支出同比
			$('#sameRatioProfit').show();//利润同比
		}else{
			$('#relativeIncome').show();//收入环比
			$('#relativeOutcome').show();//支出环比
			$('#relativeProfit').show();//利润环比
			$('#sameRatioIncome').hide();//收入同比
			$('#sameRatioOutcome').hide();//支出同比
			$('#sameRatioProfit').hide();//利润同比
		}
	}

}


function isFrom1_0() {
	if(window.top.isMobile!=undefined || window.top.isMobile!=null){
		return false;
	} else {
		return true;
	}
}
function openUrl(title, url, toType) { // toType，打开页面方式，1标签页2新页面
	var isOn1_0 = isFrom1_0();
	if (isOn1_0 == true) {
		window.open(url);
	} else {
		if (toType != null && toType != undefined) {
			if (toType == 1) {
				window.top.add_tab(title,url);
			} else {
				window.open(url);
			}
		}
	}
}


/*
 * 加载收入饼状图
 */
function loadShouru(itemArr,valueArr) {
	var data = {
		title: '收入',
		itemArr: itemArr,
		valueArr: valueArr
	};
	/*
	var data = {
			title: 'XXXXX标题',
			itemArr: ['名称1','名称2','名称3','名称4','名称5'],
			valueArr: [
                {value: 0.59, name:'名称1'},
                {value: 0.23, name:'名称2'},
                {value: 0.10, name:'名称3'},
                {value: 0.05, name:'名称4'},
                {value: 0.03, name:'名称5'}
            ]
	};

	 */
	initEcharts('id_echarts_shouru', data);
}

/*
 * 加载支出饼状图
 */
function loadZhichu(itemArr,valueArr) {
	var data = {
		title: '支出',
		itemArr: itemArr,
		valueArr: valueArr
	};
	initEcharts('id_echarts_zhichu', data);
}

/*
 * 加载利润饼状图
 */
function loadLirun(itemArr,valueArr) {

	var data = {
		title: '利润',
		itemArr: itemArr,
		valueArr: valueArr
	};
	initEcharts('id_echarts_lirun', data);
}


/**
 */
function initEcharts(id, data) {
	var option = {
		    title : {
		        text: data.title,
		        x:'center'
		    },
		/*
		    legend: {
		        orient : 'vertical',
		        x : 'left',
		        data: data.itemArr
		    },

		 */
		tooltip: {
			trigger: 'item',
			formatter: '{a} <br/>{b} : {c} ({d}%)'
		},
		    toolbox: {
		        show : true,
		        feature : {
		            mark : {show: true},
		            dataView : {show: true, readOnly: false},
		            restore : {show: true},
		            saveAsImage : {show: true}
		        }
		    },
		    calculable : true,
		    series : [
		        {
		            name:data.title+'金额',
		            type:'pie',
		            radius : '55%',
		            center: ['50%', '60%'],
		            data: data.valueArr,
					emphasis: {
						itemStyle: {
							shadowBlur: 10,
							shadowOffsetX: 0,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						}
					}
		        }
		    ]
		};
	echarts.init(document.getElementById(id)).setOption(option, true);
}



//补零函数
function zero(n) {
	return n >= 10 ? n : '0' + n
}
